package com.yifushidai.service;

import com.yifushidai.entity.TokenEntity;
import com.yifushidai.mapper.TokenEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
public class TokenService {
    @Autowired
    private TokenEntityMapper tokenMapper;
    //失效时间：12小时后过期  1年
    private final static long EXPIRE = (3600 * 24 * 365);


//	public void save(TokenEntity token){
//		tokenMapper.insertSelective(token);
//	}
//
//
//	public void update(TokenEntity token){
//		tokenMapper.updateByPrimaryKeySelective(token);
//	}


    public Map<String, Object> createToken(String mobile) {
        //生成一个token
        String token = UUID.randomUUID().toString().replace("-", "");
        System.out.println("new token1 ： " + token);
        //当前时间
        Date now = new Date();

        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        //判断是否生成过token
        TokenEntity tokenEntity = tokenMapper.selectByPrimaryKey(mobile);
        if (tokenEntity == null) {
            tokenEntity = new TokenEntity();
            tokenEntity.setMobile(mobile);
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);
            int i = tokenMapper.insertSelective(tokenEntity);
            System.out.println("插入token ： " + i);
        } else if ("17711351006".equals(mobile)) {
			token = tokenEntity.getToken();
        } else {
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);
            int j = tokenMapper.updateByPrimaryKeySelective(tokenEntity); //0 1
            System.out.println("更新token ： " + j);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        System.out.println("new token2 ： " + token);
        map.put("expire", EXPIRE);
        return map;
    }

    public TokenEntity queryByToken(String token) {
        return tokenMapper.queryByToken(token);
    }

    public TokenEntity queryByMobile(String mobile) { return tokenMapper.selectByPrimaryKey(mobile); }
}
